import { GET_USER, SET_USER, USER_ACTION, STORAGE_USER } from '@/utils/Const';
import { Commit } from 'vuex';

import Storage from '@/utils/Storage'

interface State {
  value: UserProps
}

interface UserProps {
  username: string,
  accessToken: string,
  avatar: string
}

export default {
  namespaced: true,
  state: {
    value: Storage.get(STORAGE_USER, 1) || {}
  },
  getters: {
    [GET_USER](state: State): UserProps {
      return state.value
    }
  },
  mutations: {
    [SET_USER](state: State, payload: UserProps): void {
      state.value = payload;
      Storage.set(STORAGE_USER, payload, 1);
    }
  },
  actions: {
    [USER_ACTION](context: { commit: Commit }, payload: UserProps): void {
      context.commit(SET_USER, payload)
    }
  }
}